package com.mspbots.rule.exceptions.ticket;

import com.mspbots.api.service.MessagesService;
import com.mspbots.api.service.TenantUserMappingService;
import com.mspbots.cw.request.MessagesDTO;
import com.mspbots.cw.request.TenantRuleDTO;
import com.mspbots.cw.request.TenantTicketDTO;
import com.mspbots.rule.exceptions.BaseRule;
import org.jeasy.rules.annotation.Action;
import org.jeasy.rules.annotation.Fact;
import org.springframework.scheduling.annotation.Async;

/**
 * @ClassName com.mspbots.rule.exceptions.ticket.TicketBaseRule
 * @Description TODO
 * @Author Jun
 * @Date 2019/8/13 17:36
 * @Version 1.0
 **/
public abstract class TicketBaseRule extends BaseRule implements ITicketBaseRule {

    protected static final String BUSINESS_TYPE = "ticket";


    public void setService(MessagesService messagesService,
                           TenantUserMappingService tenantUserMappingService,
                           TenantRuleDTO tenantRuleDTO) {
        this.messagesService = messagesService;
        this.tenantUserMappingService = tenantUserMappingService;
        this.tenantRuleDTO = tenantRuleDTO;
        messages = new MessagesDTO();
    }

    protected StringBuilder suffixTicketLink(Long ticketId) {
        StringBuilder messageBuilder = new StringBuilder();
        messageBuilder.append(
                "[<a href='https://" + getConfiguration(KEYWORD_WISE_SITE) + "/v4_6_release//ConnectWise.aspx?locale=en_US&routeTo=ServiceFv&recid=");
        messageBuilder.append(ticketId).append("'>");
        messageBuilder.append(ticketId).append("</a>]&nbsp;");
        return messageBuilder;
    }

    protected boolean checkMessage(TenantTicketDTO tenantTicketDTO) {
        messages.setBusinessId(tenantTicketDTO.getCwId());
        return super.checkMessage(messages);
    }

    protected boolean insertMessage() {
        messages.setBusinessType(BUSINESS_TYPE);
        return super.insertMessage(messages);
    }


    @Action
    @Async
    @Override
    public void action(@Fact(FACT) TenantTicketDTO tenantTicketDTO) {
        messages.setMessage(buildMessage(tenantTicketDTO.getUpdatedTime()));
        messages.setBusinessId(tenantTicketDTO.getId());
        insertMessage();
    }
}
